package com.example.administrator.yuejian35.Recommend.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.yuejian35.R;
import com.example.administrator.yuejian35.Recommend.adapter.SearchresultAdapter;
import com.example.administrator.yuejian35.Recommend.bean.Searchresult;
import com.example.administrator.yuejian35.Recommend.model.RecommendModel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CourseSearchActivity extends AppCompatActivity {
    private EditText etSearch;
    private ImageView ic_back;
    private ImageView cancle;
    private String getstring;
    private RecyclerView mListView;
    private List<Searchresult> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_search);
        etSearch=(EditText) findViewById(R.id.etSearch);
        cancle=(ImageView) findViewById(R.id.ivDeleteText);
        ic_back=(ImageView) findViewById(R.id.video_pic);
        mListView=(RecyclerView) findViewById(R.id.mListView);

        ic_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(editable.length() == 0){
                    cancle.setVisibility(View.GONE);//当文本框为空时，则叉叉消失  
                    //Toast.makeText(CourseSearchActivity.this,"111111",Toast.LENGTH_SHORT).show();

                    }
                else {
                    cancle.setVisibility(View.VISIBLE);//当文本框不为空时，出现叉叉  
                    cancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            etSearch.setText("");
                        }
                    });


                    getstring=etSearch.getText().toString();
                    RecommendModel recommendmodel = new RecommendModel();
                    Call call = recommendmodel.searchcourse(getstring);
                    call.enqueue(new Callback<List<Searchresult>>() {
                        @Override
                        public void onResponse(Call<List<Searchresult>> call, Response<List<Searchresult>> response) {
                            //Toast.makeText(CourseSearchActivity.this,"chenggong",Toast.LENGTH_SHORT).show();
                            if(CourseSearchActivity.this!=null) {
                                //Toast.makeText(CourseSearchActivity.this,getstring,Toast.LENGTH_SHORT).show();
                                list = response.body();
                                SearchresultAdapter adapter = new SearchresultAdapter(CourseSearchActivity.this, list);
                                //Toast.makeText(CourseSearchActivity.this,response.body().get(0).getCoursename().toString(),Toast.LENGTH_SHORT).show();
                                LinearLayoutManager layoutManager = new LinearLayoutManager(CourseSearchActivity.this);
                                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//Horizontal
                                mListView.setLayoutManager(layoutManager);
                                mListView.setAdapter(adapter);
                                mListView.setItemAnimator(new DefaultItemAnimator());
                            }
                        }

                        @Override
                        public void onFailure(Call<List<Searchresult>> call, Throwable t) {
                            Toast.makeText(CourseSearchActivity.this,"shibai", Toast.LENGTH_SHORT).show();
                        }
                    });

                    }

            }
        });
    }

}
